package apps.softtech.fastmarket.adapter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

import apps.softtech.Utility.BitmapManager;
import apps.softtech.fastmarket.R;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;
import apps.softtech.fastmarket.model.Product;

public class AdapterListProduct1 extends BaseAdapter {

	Activity mActivity;
	Context mContext;
	LayoutInflater inflater;

	/**
	 * List contain product
	 */
	List<Product> mListProduct = new ArrayList<Product>();

	Product mProduct;

	/**
	 * layout containt listview product
	 */
	private LinearLayout mLayout;
	private LayoutParams params;
	/*
	 * bien dung de load anh
	 */
	BitmapManager mBitmapManager;
	DisplayImageOptions options;

	public AdapterListProduct1(Context context, List<Product> listProduct,
			DisplayImageOptions options) {

		mContext = context;
		mListProduct = listProduct;
		mBitmapManager = new BitmapManager();
		mBitmapManager.setPlaceholder(BitmapFactory.decodeResource(
				mContext.getResources(), R.drawable.ic_launcher));
		this.options = options;
		inflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		if(mListProduct.size()<=0){
			return 1;
		}
		return mListProduct.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewholder = new ViewHolder();
		if (mListProduct.size() == 0 || mListProduct.equals(null)) {
			convertView = inflater.inflate(R.layout.item_list_product_home,
					null);
			return convertView;
		}
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_list_product_home,
					null);
			initViewHolder(convertView, viewholder);
			convertView.setTag(viewholder);
		} else {
			viewholder = (ViewHolder) convertView.getTag();
		}

		mProduct = mListProduct.get(position);

		viewholder.tvDescription.setText(mProduct.getDescription());
		// viewholder.tvName.setText(mProduct.getCreatedDate());
		Log.v("adasdasd",mListProduct.get(position).getAvatarPath() );
		ImageLoader.getInstance().displayImage(
				mListProduct.get(position).getAvatarPath(), viewholder.img,options);
		
		return convertView;
	}

	private void initViewHolder(View v, ViewHolder holder) {
		holder.img = (ImageView) v.findViewById(R.id.imgProduct);
		holder.tvDescription = (TextView) v.findViewById(R.id.tvDescription);
		holder.tvName = (TextView) v.findViewById(R.id.tvName);
		holder.tvPrice = (TextView) v.findViewById(R.id.tvPrice);
	}

	class ViewHolder {
		ImageView img;
		TextView tvName;
		TextView tvDescription;
		TextView tvPrice;

	}

	static final class AnimateFirstDisplayListener extends
			SimpleImageLoadingListener {

		static final List<String> displayedImages = Collections
				.synchronizedList(new LinkedList<String>());

		@Override
		public void onLoadingComplete(String imageUri, View view,
				Bitmap loadedImage) {
			if (loadedImage != null) {
				ImageView imageView = (ImageView) view;
				boolean firstDisplay = !displayedImages.contains(imageUri);
				if (firstDisplay) {
					FadeInBitmapDisplayer.animate(imageView, 500);
					displayedImages.add(imageUri);
				}
			}
		}
	}
}
